import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.javalab.recorder.audio.AudioCaptureThread;
import com.javalab.recorder.audio.AudioDevice;
import com.javalab.recorder.audio.AudioPlaybackThread;
import com.javalab.recorder.listener.OptionsMenuItemClickEventListener;
import com.javalab.recorder.listener.PlaybackButtonClickEventListener;
import com.javalab.recorder.listener.RecordButtonClickEventListener;
import com.javalab.recorder.listener.StopButtonClickEventListener;
import com.javalab.recorder.state.PlayingState;
import com.javalab.recorder.state.RecordingState;
import com.javalab.recorder.state.StoppedState;
import com.javalab.recorder.ui.MainFrame;


public class Bootstrap {
	private static Logger logger = Logger.getLogger(Bootstrap.class);

	public static void main(String[] args) {
		PropertyConfigurator.configure("etc\\log4j.properties");

		MainFrame mainFrame = new MainFrame();

		//Initialize capture thread
		logger.info("Initialize AudioCaptureThread.");
		AudioCaptureThread audioCaptureThread = new AudioCaptureThread(AudioDevice.getInstance().getDefaultTargetDataLine(), AudioDevice.getInstance().getDefaultAudioFormat());
		logger.debug("Initialize AudioCaptureThread complete.");

		//Initialize play back thread
		logger.info("Initialize PlaybackThread.");
		AudioPlaybackThread audioPlaybackThread = new AudioPlaybackThread();
		audioPlaybackThread.setMainFrame(mainFrame);
		logger.debug("Initialize Playback Thread complete.");

		//Initialize state
		logger.info("Construct RecorderState.");
		RecordingState.getInstance().setMainFrame(mainFrame);
		RecordingState.getInstance().setAudioCaptureThread(audioCaptureThread);
		PlayingState.getInstance().setMainFrame(mainFrame);
		PlayingState.getInstance().setAudioPlaybackThread(audioPlaybackThread);
		StoppedState.getInstance().setMainFrame(mainFrame);
		StoppedState.getInstance().setAudioCaptureThread(audioCaptureThread);
		StoppedState.getInstance().setAudioPlaybackThread(audioPlaybackThread);
		logger.debug("Construct RecorderState complete.");
		mainFrame.setRecorderState(StoppedState.getInstance());

		logger.info("Register event listener for MainFrame.");
		mainFrame.subscribeEvent(new RecordButtonClickEventListener());
		mainFrame.subscribeEvent(new PlaybackButtonClickEventListener());
		mainFrame.subscribeEvent(new StopButtonClickEventListener());
		mainFrame.subscribeEvent(new OptionsMenuItemClickEventListener());
		logger.debug("Register event listener for MainFrame complete.");
		mainFrame.setVisible(true);
	}
}
